package com.gxgeek.spring.security.securitycore.validate;

import com.gxgeek.spring.security.securitycore.properties.SecurityConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.ServletWebRequest;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author gaoxing
 * @create 2017-10-17 下午9:24
 */
@RestController
public class ValidateCodeController {

     public static final String sessionKey = "SESSION_KEY_IMAGE_CODE";

    @Autowired
    private ValidateCodeProcessorHolder handler;

//    @GetMapping("/code/image")
//    public void createImageCode(HttpServletRequest request, HttpServletResponse response) throws IOException {
//        ImageCode imageCode = (ImageCode) imageValidateCodeGenerator.generatorCode(new ServletWebRequest(request));
//        sessionStrategy.setAttribute(new ServletWebRequest(request), sessionKey, imageCode);
//        ImageIO.write(imageCode.getImage(), "JPEG", response.getOutputStream());
//    }
//    @GetMapping("/code/sms")
//    public void createSmsCode(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletRequestBindingException {
//        ValidateCode validateCode = smsCodeGenerator.generatorCode(new ServletWebRequest(request));
//        sessionStrategy.setAttribute(new ServletWebRequest(request), sessionKey, validateCode);
//        String mobile = ServletRequestUtils.getStringParameter(request,"mobile");
//        smsCodeSender.send(mobile,validateCode.getCode());
//    }

    @GetMapping(SecurityConstants.DEFAULT_VALIDATE_CODE_URL_PREFIX+"/{type}")
    public void createCode(HttpServletRequest request, HttpServletResponse response, @PathVariable String type) throws Exception {
        handler.findValidateCodeProcessor(type).create(new ServletWebRequest(request,response));
    }



}
